﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCF20Model;
using System.Windows;
using System.Collections;
using WiseLink.RDS.Resource;

namespace CCF20Module.DeviceState
{
    public interface IDeviceHandler
    {
        Device CurrentDevice { get; set; }
        IDeviceState CurrentState { get; set; }
        IDeviceState getDeliverState(); //在途
        IDeviceState getInStockState();//库存
        IDeviceState getSellToCustomerState();//售出
        IDeviceState getDefectedState();//损坏

        void setState(IDeviceState deviceState);
        void InitializeState();
    }
    public class DeviceHandler : IDeviceHandler
    {
        public  Device CurrentDevice { get; set; }
        public IDeviceState CurrentState { get; set; }
        public DeviceModel deviceModel {get;set;}
        public VehicleModel vehicleModel { get; set; }
        IDeviceState inStockState;
        IDeviceState deliverState;
        IDeviceState sellToCustomerState;
        IDeviceState defectedState;
        IList sourceList;
        public DeviceHandler(DeviceModel model,VehicleModel vModel,Device device,IList list)
        {
            CurrentDevice = device;
            deviceModel = model;
            vehicleModel = vModel;
            sourceList = list;
            InitializeState();
        }
        public void InitializeState()
        {
            if (CurrentDevice.DeviceStatus == DeviceStatus.InStock)
            {
                    setState(getInStockState());
            }else if (CurrentDevice.DeviceStatus == DeviceStatus.Sold)
            {
                    setState(getSellToCustomerState());
            }else if (CurrentDevice.DeviceStatus == DeviceStatus.Shipped)
            {
                    setState(getDeliverState());
            }else if (CurrentDevice.DeviceStatus == DeviceStatus.Repair)
            {
                    setState(getDefectedState()); //设备损坏是不可以做任何事
            }else
            {
                    setState(getInStockState());
                    //LoggerHelper.LogCCF20(string.Format("未知设备IDC:{0}状态:{1}", CurrentDevice.IDCSimNumber, CurrentDevice.DeviceStatus));
             
            }
            
        }
        private void ShowMessageToCustomer(string msg)
        {
            if (!string.IsNullOrEmpty(msg))
            {
                Utility.ShowMessageBox(msg);
            }
        }

        #region Execute State Operation

        public void InStock()
        {
            ShowMessageToCustomer(CurrentState.runInStock());
        }
        public void RegisterIDC()
        {
            ShowMessageToCustomer(CurrentState.runRegisterIDC());
        }
        public void RegisterIDC(string SN, string SNKey, string IDSName, string IDSID)
        {
            ShowMessageToCustomer(CurrentState.runRegisterIDC(SN, SNKey, IDSName, IDSID));
        }
        public void UnRegisterIDC()
        {
            ShowMessageToCustomer(CurrentState.runUnRegisterIDC());
        }
        public void RegisterSpecialIDC()
        {
            ShowMessageToCustomer(CurrentState.runRegisterSpeicalIDC());
        }
        public void UnRegisterSpecialIDC()
        {
            ShowMessageToCustomer(CurrentState.runUnRegisterSpeicalIDC());
        }
        public void DeleteDevice()
        {
            ShowMessageToCustomer(CurrentState.runDeleteDevice());
        }
        public bool ExchangeDevice(string idc, string deviceKey)
        {
            string response = CurrentState.runExchangeDevice(idc, deviceKey);
            ShowMessageToCustomer(response);
            if (response.Contains(Message.Success))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void RepairStart()
        {
            ShowMessageToCustomer(CurrentState.runRepairStart());
        }
        public void RepairEnd()
        {
            ShowMessageToCustomer(CurrentState.runRepairEnd());
        }
        public void ReturnDevice()
        {
            ShowMessageToCustomer(CurrentState.runReturnDevice());
        }
        public void SellToCustomer(int customerId)
        {
            ShowMessageToCustomer(CurrentState.runSellToCustomer(customerId));
        }

        #endregion

        #region IDeviceState GetState
        public IDeviceState getInStockState()
        {
            inStockState = new InStockState(this, deviceModel,vehicleModel, CurrentDevice, sourceList);
            return inStockState;
        }

        public IDeviceState getSellToCustomerState()
        {
            sellToCustomerState = new SellToCustomerState(this, deviceModel, vehicleModel, CurrentDevice, sourceList);
            return sellToCustomerState;
        }

        public IDeviceState getDeliverState()
        {
            deliverState = new DeliverState(this, deviceModel, vehicleModel, CurrentDevice, sourceList);
            return deliverState;
        }
        public IDeviceState getDefectedState()
        {
            defectedState = new DefectedState(this, deviceModel, vehicleModel, CurrentDevice, sourceList);
            return defectedState;
        }

#endregion


        public void setState(IDeviceState deviceState)
        {
            CurrentState = deviceState;
            LoggerHelper.LogCCF20(string.Format("SetState:{0} IDC:{1}",deviceState.StateName,CurrentDevice.IDCSimNumber));
        }
    }
}
